package com.xcm.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.xcm.constant.PageConstant;
import com.xcm.exception.ControllerException;
import com.xcm.model.dto.ProgramInfoListDTO;
import com.xcm.service.ProgramInfoService;

@Controller
@RequestMapping(value = "/programInfo")
public class ProgramInfoController {
	private static Logger logger = LoggerFactory.getLogger(ProgramInfoController.class);
	@Autowired
	private ProgramInfoService programInfoService;
	
	/**
	 * 节目详情列表页
	 */
	@RequestMapping(value = "/listPage")
	public String getProgramJsons( Model model,String version ) {
		model.addAttribute("version",version);
		return "program_details";
	}
	
	
	/**
	 * 节目详情列表 ajax get Json Data
	 * @param version 版本号
	 * @param deviceNum 机器码
	 * @param pageNum
	 * @param pageSize
	 * @return
	 * @throws ControllerException 
	 */
	@RequestMapping(value = "/getProgramInfo")
	@ResponseBody
	public Object getProgramInfo(String version, String deviceNum, Integer pageNum, Integer pageSize) {
		Map<String,Object> map = new HashMap<>();
		if (StringUtils.isBlank(version)) {
			return new ControllerException("版本号参数不能为空");
		}
		pageNum = ( pageNum == null ? PageConstant.DEFAULT_PAGE_NUM : pageNum ) ;
		pageSize = ( pageSize == null ? PageConstant.DEFAULT_PAGE_SIZE : pageSize );
		Page<ProgramInfoListDTO> page = programInfoService.listProgramInfoByPage(version, deviceNum, pageNum, pageSize);
		map.put("record", page.getResult());
		map.put("pageNum", page.getPageNum());
		map.put("pageSize", page.getPageSize());
		map.put("total", page.getTotal());
		return map;
	}
}
